<?php if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of main
 *
 * @author Maulnick
 */
class Main extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->helper('url','form');
        $this->load->model('User_Model');
        $this->load->model('Message_Model');
        $this->load->model('Notification_Model');
        $this->load->library('session');
    }
    /*** 
     * 
     * /Main/function_page()
     * 
     * ***/
    public function index() {
        $this->home_page();
    }
    public function home_page(){
        $title = 'home';
        // check session
        $tempsession = $this->session->userdata('user');
        if($tempsession){
            // if login
            $leftpage['page'] = 'loggedin';
            $leftpage['userdata'] = $this->User_Model->get_user($tempsession['role_id'],$tempsession['user_id']);
            $middlepage['page'] = 'home';
            $rightpage['page'] = 'notification';
            $rightpage['page'] = 'notification';
            $rightpage['notification'] = $this->Notification_Model->get_notification($tempsession['role_id'],$tempsession['user_id']);
        
        }else{
            // if not login
            $leftpage['page'] = 'login';
            $middlepage['page'] = 'home';
            $rightpage['page'] = 'blank';
        }
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
        $this->session->unset_userdata('active_page');
        $this->session->set_userdata('active_page', 'home');
    }
    public function task_page(){
        $title = 'task';
        $tempsession = $this->session->userdata('user');
        if($tempsession){
            $leftpage['page'] = 'loggedin';
            $leftpage['userdata'] = $this->User_Model->get_user($tempsession['role_id'],$tempsession['user_id']);
            $middlepage['page'] = 'task';
            $rightpage['page'] = 'notification';
            $rightpage['notification'] = $this->Notification_Model->get_notification($tempsession['role_id'],$tempsession['user_id']);
        }else{
            show_404();
            return;
        }
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
    }
    public function about_page(){
        $title = 'about';
        // check session
        $tempsession = $this->session->userdata('user');
        if($tempsession){
            // if login
            $leftpage['page'] = 'loggedin';
            $leftpage['userdata'] = $this->User_Model->get_user($tempsession['role_id'],$tempsession['user_id']);
            $middlepage['page'] = 'about';
            $rightpage['page'] = 'notification';
            $rightpage['notification'] = $this->Notification_Model->get_notification($tempsession['role_id'],$tempsession['user_id']);
        
        }else{
            // if not login
            $leftpage['page'] = 'login';
            $middlepage['page'] = 'about';
            $rightpage['page'] = 'blank';
        }
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
        $this->session->unset_userdata('active_page');
        $this->session->set_userdata('active_page', 'about');
    }
    public function contact_page(){
        $title = 'contact';
        // check session
        $tempsession = $this->session->userdata('user');
        if($tempsession){
            // if login
            $leftpage['page'] = 'loggedin';
            $leftpage['userdata'] = $this->User_Model->get_user($tempsession['role_id'],$tempsession['user_id']);
            $middlepage['page'] = 'contact';
            $rightpage['page'] = 'notification';
            $rightpage['notification'] = $this->Notification_Model->get_notification($tempsession['role_id'],$tempsession['user_id']);
        }else{
            // if not login
            $leftpage['page'] = 'login';
            $middlepage['page'] = 'contact';
            $rightpage['page'] = 'blank';
        }
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
        $this->session->unset_userdata('active_page');
        $this->session->set_userdata('active_page', 'contact');
    }
    public function notification_page(){
        $title = 'notification';
        // check session
        $tempsession = $this->session->userdata('user');
        
        $leftpage['page'] = 'loggedin';
        $leftpage['userdata'] = $this->User_Model->get_user($tempsession['role_id'],$tempsession['user_id']);
        $middlepage['page'] = 'notification';
        $middlepage['notification'] = $this->Notification_Model->get_all_notification($tempsession['role_id'],$tempsession['user_id']);
        $rightpage['page'] = 'blank';
        
        $this->show_page($title, $leftpage, $middlepage, $rightpage);
        $this->session->unset_userdata('active_page');
        $this->session->set_userdata('active_page', 'notification');
    }
    /*** 
     * 
     * /Main/function()
     * 
     * ***/
    private function show_page($title, $leftpage, $middlepage, $rightpage){
        $data['title'] = $title;
        $data['topbar'] = $this->load->view('topbar/home','',TRUE);
        $data['rightpage'] = $this->load->view('mainpage/bodyright',$rightpage,TRUE);
        $data['middlepage'] = $this->load->view('mainpage/bodymiddle',$middlepage,TRUE);
        $data['leftpage'] = $this->load->view('mainpage/bodyleft',$leftpage,TRUE);
        $this->load->view('front',$data);
    }
}
